package com.example.login.controller;

import com.example.login.entity.User;
import com.example.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class loginController {

    @Autowired
    UserService userService;

    @RequestMapping("/user/login")
    public String login(Model model, HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password) {
        User user = new User(username, password);
        User loginUser = userService.checkLogin(user, model);
        if (loginUser == null) {
            return "index";
        } else {
            session.setAttribute("user", loginUser);
            return "loginSuccess";
        }
    }

    @RequestMapping("/user/toReg")
    public String toReg() {
        return "register";
    }


}
